// 格式化距离显示
export const formatDistance = (distance) => {
    if (distance === null) return '未知';
    if (distance < 1) {
        return `${(distance * 1000).toFixed(0)}m`;
    }
    return `${distance.toFixed(1)}km`;
};

// 判断商店是否营业中
export const isStoreOpen = (businessHours) => {
    if (!businessHours) return false;

    const now = new Date();
    const dayOfWeek = now.getDay(); // 0是周日，1-6是周一到周六
    const currentHour = now.getHours();
    const currentMinute = now.getMinutes();

    try {
        // 假设businessHours格式为: "1-5:9:00-21:00,6-7:10:00-22:00"
        const schedules = businessHours.split(',');

        for (const schedule of schedules) {
            const firstColonIndex = schedule.indexOf(':');
            if (firstColonIndex === -1) {
                continue; // 跳过格式不正确的计划
            }
            const daysPart = schedule.substring(0, firstColonIndex);
            const hoursPart = schedule.substring(firstColonIndex + 1);

            const [dayStart, dayEnd] = daysPart.split('-').map(Number);

            // 统一星期表示: JS的周日是0, 后端可能是7.
            const today = dayOfWeek === 0 ? 7 : dayOfWeek;

            if (today >= dayStart && today <= dayEnd) {
                const [timeStart, timeEnd] = hoursPart.split('-');
                const [startHour, startMinute] = timeStart.split(':').map(Number);
                const [endHour, endMinute] = timeEnd.split(':').map(Number);

                const currentTime = currentHour * 60 + currentMinute;
                const openTime = startHour * 60 + startMinute;
                const closeTime = endHour * 60 + endMinute;

                return currentTime >= openTime && currentTime <= closeTime;
            }
        }
    } catch (error) {
        console.error('解析营业时间出错:', error);
    }

    return false;
}; 